/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, template, Collection, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },
        fetch: function (_args) {
            var _this = this;
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;

            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'withdraw',
                condition: args,
                model: {
                    tag_name: 'tr',
                    event: {},
                    change: function (e) {
                        this.data[$(e.target).attr('name')] = $(e.target).val();
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            $('#start_time').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

            $('#end_time').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });

            var _this = this;

            var reloadData = function() {
                var startTime = $('#start_time').val();
                var endTime = $('#end_time').val();
                var status = $('.btn-group .active').attr('data-item');

                _this.collection.condition.status = status;
                _this.collection.condition.start_time = startTime;
                _this.collection.condition.end_time = endTime;
                _this.collection.reload();
            }

            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();

                reloadData();
            });

            $('.btn-group button').on('click', function(e) {
                $('.btn-group button').removeClass('active');
                $(this).addClass('active');

                if ($(this).attr('data-item') == 2) {
                    $('#withdraw').show();
                } else {
                    $('#withdraw').hide();
                }

                reloadData();

            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});